package cn.tansanqinger.ThreadPool;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class NewSingleThreadScheduledExecutorDemo {
    public static void main(String[] args) {
        //创建单线程可执行周期性任务的线程池
        ScheduledExecutorService singleScheduledThreadPool = Executors.newSingleThreadScheduledExecutor();
        //提交 3 个固定频率执行的任务
        for (int i = 0; i <3; i++) {
            //scheduleWithFixedDelay 固定的延迟时间执行任务； scheduleAtFixedRate 固定的频率执行任务
            singleScheduledThreadPool.scheduleAtFixedRate(() -> {
                System.out.println(Thread.currentThread().getName());
            }, 0, 3, TimeUnit.SECONDS);
        }
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {}
        System.out.println("10毫秒后...");
        singleScheduledThreadPool.shutdown();
    }
}
